﻿// 药品管理

// 解析URL
var parameter = window.location.href.split("?")[1];
var parameterList = parameter.split("&");
var username = decodeURIComponent(parameterList[0]);  //用户名
var hospital = decodeURIComponent(parameterList[1]);  //医院
var delivery = decodeURIComponent(parameterList[2]);  //配送方式
var searchword = decodeURIComponent(parameterList[3]);  //搜索词
var pageNo  = decodeURIComponent(parameterList[4]);  //页码
var pageSize = decodeURIComponent(parameterList[5]);  //每页显示数据数目
console.log(parameterList);
$("#hospital").val(hospital);
$("#delivery").val(delivery);
$("#search_input").val(searchword);
$("#pageSize").val(pageSize+"条/页");

// 查询药品信息
$.ajax({
    type: "post",
    url: "/ysfwpt/selectDrugByInputList",
    data: {
        hospital: hospital,
        delivery: delivery,
        searchword: searchword
    },
    success: function(data) {
        // console.log("success");
        console.log(data);
        getDrugList(data);
    },
    error: function (data) {
        // console.log("error");
        // console.log(data);
    }
});
var getDrugList = function (data) {
    drugList = data.list;
    length = drugList.length;
    start = (pageNo - 1) * pageSize;
    end = pageNo * pageSize;
    pageCount = Math.ceil(length / pageSize);
    if(end >= length){
        end = length;
    }
    // console.log("总条数："+length);
    // console.log("总页数："+pageCount);
    // console.log("当前页数："+pageNo);
    // console.log("一页数目："+pageSize);
    // console.log("显示第"+start+"条到第"+end+"条数据");
    // $("#pageCount").html("第"+pageNo+"页|共"+pageCount+"页");
    $("#pageNo").val(pageNo);
    $("#totalcount").html("当前共"+length+"条记录");
    if(pageNo == 1){
        $("#pre").removeClass("paging").addClass("notAllowed");
    }
    if(pageNo == pageCount){
        $("#next").removeClass("paging").addClass("notAllowed");
    }
    if(length == 0){
        insertEmpty();
    }
    else{
        insertHtml(start,end);
    }
};
var insertEmpty = function () {
    var strhtml = '<tr>';
    strhtml = strhtml + '<td colspan="12">暂无数据</td>';
    $("#druglist").append(strhtml);
};
var insertHtml = function (start,end) {
    for (var i = start; i < end; i++) {
        var no = parseInt(i)+1;
        var druginfo = drugList[i];
        var strhtml = '<tr id="' + i + '" arrId="' + i + '">';
        strhtml = strhtml + '<td><input type="checkbox" id="checkbox" class="checkbox" style="width: 20px;height: 20px;"></td>';
        strhtml = strhtml + '<td>' + no + '</td>';
        strhtml = strhtml + '<td>' + druginfo.drug_id + '</td>';
        strhtml = strhtml + '<td title=' + druginfo.drug_name + '>' + druginfo.drug_name + '</td>';
        strhtml = strhtml + '<td>' + druginfo.drug_unit + '</td>';
        strhtml = strhtml + '<td title=' + druginfo.drug_capacity + '>' + druginfo.drug_capacity + '</td>';
        strhtml = strhtml + '<td>' + druginfo.drug_price + '</td>';
        strhtml = strhtml + '<td title=' + druginfo.drug_manufacturer + '>' + druginfo.drug_manufacturer + '</td>';
        strhtml = strhtml + '<td>' + druginfo.drug_approval_number + '</td>';
        strhtml = strhtml + '<td><button id="drugDetail" class="operation">详情</button><button id="updateDrug" class="operation">修改</button></td>';
        $("#druglist").append(strhtml);
    }
    //详情
    $("#druglist").on("click","tr td #drugDetail",function () {
        $('#detail_popup').show();
        $(".shandow").show().css('z-index', '11');
        $('body').css({
            "overflow-x":"hidden",
            "overflow-y":"hidden"
        });
        var i  = $(this).parent().parent().attr("arrId");
        var druginfo = drugList[i];
        $("#detail_drug_id").text(druginfo.drug_id);
        $("#detail_drug_name").text(druginfo.drug_name);
        $("#detail_drug_capacity").text(druginfo.drug_capacity);
        $("#detail_drug_unit").text(druginfo.drug_unit);
        $("#detail_drug_manufacturer").text(druginfo.drug_manufacturer);
        $("#detail_drug_approval_number").text(druginfo.drug_approval_number);
        $("#detail_drug_price").text(druginfo.drug_price + "元");
        $("#detail_delivery").text(druginfo.delivery);
        if(druginfo.is_ephedrine == 0){
            $("#detail_is_ephedrine").text("不含");
        }
        else{
            $("#detail_is_ephedrine").text("含");
        }
        if(druginfo.is_prescription == 0){
            $("#detail_is_prescription").text("非处方药");
        }
        else{
            $("#detail_is_prescription").text("处方药");
        }
    });
    //修改
    $("#druglist").on("click","tr td #updateDrug",function () {
        $('#update_popup').show();
        $(".shandow").show().css('z-index', '11');
        $('body').css({
            "overflow-x":"hidden",
            "overflow-y":"hidden"
        });
        var i  = $(this).parent().parent().attr("arrId");
        var druginfo = drugList[i];
        $("#update_drug_id").text(druginfo.drug_id);
        $("#update_drug_name").val(druginfo.drug_name);
        $("#update_drug_capacity").val(druginfo.drug_capacity);
        $("#update_drug_unit").val(druginfo.drug_unit);
        $("#update_drug_manufacturer").val(druginfo.drug_manufacturer);
        $("#update_drug_approval_number").val(druginfo.drug_approval_number);
        $("#update_drug_price").val(druginfo.drug_price);
        $("#update_delivery").val(druginfo.delivery);
        if(druginfo.is_ephedrine == 0){
            $("#update_is_ephedrine").val("不含");
        }
        else{
            $("#update_is_ephedrine").val("含");
        }
        if(druginfo.is_prescription == 0){
            $("#update_is_prescription").val("非处方药");
        }
        else{
            $("#update_is_prescription").val("处方药");
        }
    });
};

var pId = [];
var pid;
// 全选
$("#all").click(function () {
    // 判断全选按钮的选中状态
    var xz = $(this).prop("checked");
    // 让class名为checkbox的选项的选中状态和全选按钮的选中状态一致
    $(".checkbox").prop("checked",xz);
    $(".checkbox").each(function() {
        if (this.checked) {
            var i = $(this).parent().parent().attr("arrId");
            if(pId.indexOf(drugList[i].drug_id) == -1){
                pId.push(drugList[i].drug_id);
            }
        }
        else{
            pId.splice(0,pId.length);
        }
    });
});
// 获取勾选的数据
$("#druglist").on("click","tr td #checkbox",function () {
    var i = $(this).parent().parent().attr("arrId");
    if($(this).is(':checked')) {
        if(pId.indexOf(drugList[i].drug_id) == -1){
            pId.push(drugList[i].drug_id);
        }
    }
    else{
        pId.splice($.inArray(drugList[i].drug_id,pId),1);
    }
});

// 批量删除
$("#delete").bind("click", function() {
    if(pId.length == 0){
        alert("请勾选要批量删除的药品！");
    }
    else{
        var r=confirm("请确认是否批量删除药品？");
        if (r==true){
            for(var n = 0;n < pId.length;n ++) {
                pid = pId[n];
                $.ajax({
                    type: "post",
                    url: "/ysfwpt/deleteDrug",
                    data: {
                        drug_id: pid
                    },
                    success: function (data) {
                        // console.log("success");
                        // console.log(data);
                    },
                    error: function (data) {
                        // console.log("error");
                        // console.log(data);
                    }
                });
            }
            refresh();
        }
    }
});

// 设置下拉框宽度（输入框id1，下拉框id2）
var setWidth = function (id1,id2) {
    var width = $("#" + id1).css('width');
    $("#" + id2).css({
        "width": width
    });
};
// 点击搜索框
$("#delivery").parent().click(function (e){
    // e.stopPropagation(); //阻止冒泡 
    if($("#delivery_list").is(':visible')){
        $("#delivery_list").slideUp(300);
        $(this).find(".icon").css("transform","rotate(0deg)");
    }
    else{
        $(".search_input_option").slideUp(300);
        $(".icon").css("transform","rotate(0deg)");
        setWidth("delivery","delivery_list div");
        $("#delivery_list").slideDown(300);
        $(this).find(".icon").css("transform","rotate(180deg)");
        var obj_lis = $("#delivery_list ul li");
        for(var i = 0;i < obj_lis.length;i ++){
            obj_lis[i].onclick = function () {
                $("#delivery").val(this.innerHTML);
                $("#delivery_list").slideUp(300);
                $(this).find(".icon").css("transform","rotate(0deg)");
            }
        }
    }
});
$("#pageSize").click(function (e){
    e.stopPropagation(); //阻止冒泡 
    if($("#pageSize_list").is(':visible')){
        $("#pageSize_list").slideUp(300);
    }
    else{
        $("#pageSize_list").slideDown(300);
        var obj_lis = $("#pageSize_list ul li");
        for(var i = 0;i < obj_lis.length;i ++){
            obj_lis[i].onclick = function () {
                pageSize = this.className;
                pageNo = 1;
                $("#pageSize").val(this.innerHTML);
                $("#pageSize_list").slideUp(300);
                refresh();
            }
        }
    }
});

// 搜索框点击任意选项后进行搜索
var object;
$(document).click(function(e) {
    object = $(e.target).attr("class");
    if(object == "list" || object == "state state_chose"){
        // 提交数据自动进行搜索
        refresh();
    }
});

// 点击×清空输入框，并重新搜索刷新页面
$(".icon_clear").click(function () {
    $(this).parent().find('input').val("");
    refresh();
});

//点击搜索
$("#search_button").click(function (){
    searchword = $("#search_input").val();
    refresh();
});

// 点击上一页/下一页
$("#pre").click(function (){
    if(pageNo == 1){
        console.log("首页")
    }
    else{
        pageNo = parseInt(pageNo) - parseInt(1);
        refresh();
    }
});
$("#next").click(function (){
    if(pageNo == pageCount){
        console.log("尾页")
    }
    else{
        pageNo = parseInt(pageNo) + parseInt(1);
        refresh();
    }
});

// 页码跳转输入框失去焦点后跳转页面
$("#pageNo").blur( function(){
    // console.log(pageNo);
    // 页码发生改变
    if(pageNo != $(this).val()){
        // 页码小于最小页码
        if($(this).val() < 1){
            pageNo = 1;
        }
        // 页码大于最大页码
        else if($(this).val() > pageCount){
            pageNo = pageCount;
        }
        else{
            pageNo = $(this).val();
        }
        refresh();
    }
});

// 确定详情
$("#detail_popup_top div,#detail_popup_bottom button:nth-child(1)").bind("click", function() {
    $("#detail_popup").hide();
    $(".shandow").hide().css('z-index','-1');
    $('body').css({
        "overflow-x":"auto",
        "overflow-y":"auto"
    });
});

//编辑下拉框
$("#update_delivery").parent().click(function (e){
    // e.stopPropagation(); //阻止冒泡 
    if($("#update_delivery_list").is(':visible')){
        $("#update_delivery_list").slideUp(300);
        $(this).find(".icon").css("transform","rotate(0deg)");
    }
    else{
        $(".search_input_option").slideUp(300);
        $(".icon").css("transform","rotate(0deg)");
        setWidth("update_delivery","update_delivery_list div");
        $("#update_delivery_list").slideDown(300);
        $(this).find(".icon").css("transform","rotate(180deg)");
        var obj_lis = $("#update_delivery_list ul li");
        for(var i = 0;i < obj_lis.length;i ++){
            obj_lis[i].onclick = function () {
                $("#update_delivery").val(this.innerHTML);
                $("#update_delivery_list").slideUp(300);
                $(this).find(".icon").css("transform","rotate(0deg)");
            }
        }
    }
});
$("#update_is_ephedrine").parent().click(function (e){
    // e.stopPropagation(); //阻止冒泡 
    if($("#update_is_ephedrine_list").is(':visible')){
        $("#update_is_ephedrine_list").slideUp(300);
        $(this).find(".icon").css("transform","rotate(0deg)");
    }
    else{
        $(".search_input_option").slideUp(300);
        $(".icon").css("transform","rotate(0deg)");
        setWidth("update_is_ephedrine","update_is_ephedrine_list div");
        $("#update_is_ephedrine_list").slideDown(300);
        $(this).find(".icon").css("transform","rotate(180deg)");
        var obj_lis = $("#update_is_ephedrine_list ul li");
        for(var i = 0;i < obj_lis.length;i ++){
            obj_lis[i].onclick = function () {
                $("#update_is_ephedrine").val(this.innerHTML);
                $("#update_is_ephedrine_list").slideUp(300);
                $(this).find(".icon").css("transform","rotate(0deg)");
            }
        }
    }
});
$("#update_is_prescription").parent().click(function (e){
    // e.stopPropagation(); //阻止冒泡 
    if($("#update_is_prescription_list").is(':visible')){
        $("#update_is_prescription_list").slideUp(300);
        $(this).find(".icon").css("transform","rotate(0deg)");
    }
    else{
        $(".search_input_option").slideUp(300);
        $(".icon").css("transform","rotate(0deg)");
        setWidth("update_is_prescription","update_is_prescription_list div");
        $("#update_is_prescription_list").slideDown(300);
        $(this).find(".icon").css("transform","rotate(180deg)");
        var obj_lis = $("#update_is_prescription_list ul li");
        for(var i = 0;i < obj_lis.length;i ++){
            obj_lis[i].onclick = function () {
                $("#update_is_prescription").val(this.innerHTML);
                $("#update_is_prescription_list").slideUp(300);
                $(this).find(".icon").css("transform","rotate(0deg)");
            }
        }
    }
});
// 取消编辑
$("#update_popup_top div,#update_popup_bottom button:nth-child(2)").bind("click", function() {
    $("#update_popup").hide();
    $(".shandow").hide().css('z-index','-1');
    $('body').css({
        "overflow-x":"auto",
        "overflow-y":"auto"
    });
});
//确定编辑
$("#update_popup_bottom button:nth-child(1)").bind("click", function() {
    $("#update_popup").hide();
    $(".shandow").hide().css('z-index','-1');
    var drug_id = $("#update_drug_id").text();
    var drug_name = $("#update_drug_name").val();
    var drug_capacity = $("#update_drug_capacity").val();
    var drug_unit = $("#update_drug_unit").val();
    var drug_price = $("#update_drug_price").val();
    var drug_manufacturer = $("#update_drug_manufacturer").val();
    var drug_approval_number = $("#update_drug_approval_number").val();
    var is_ephedrine = $("#update_is_ephedrine").val();
    var is_prescription = $("#update_is_prescription").val();
    var delivery = $("#update_delivery").val();
    switch (is_ephedrine) {
        case "不含":
            is_ephedrine = 0;
            break;
        case "含":
            is_ephedrine = 1;
            break;
    }
    switch (is_prescription) {
        case "非处方药":
            is_prescription = 0;
            break;
        case "处方药":
            is_prescription = 1;
            break;
    }
    $.ajax({
        type: "post",
        url: "/ysfwpt/updateDrug",
        data: {
            drug_id: drug_id,
            drug_name: drug_name,
            drug_capacity: drug_capacity,
            drug_unit: drug_unit,
            drug_price: drug_price,
            drug_manufacturer: drug_manufacturer,
            drug_approval_number: drug_approval_number,
            hospital: "测试医院",
            is_ephedrine: is_ephedrine,
            is_prescription: is_prescription,
            delivery: delivery
        },
        success: function (data) {
            // console.log("success");
            // console.log(data);
            refresh();
        },
        error: function (data) {
            // console.log("error");
            // console.log(data);
        }
    });
});

//新增下拉框
$("#add_delivery").parent().click(function (e){
    // e.stopPropagation(); //阻止冒泡 
    if($("#add_delivery_list").is(':visible')){
        $("#add_delivery_list").slideUp(300);
        $(this).find(".icon").css("transform","rotate(0deg)");
    }
    else{
        $(".search_input_option").slideUp(300);
        $(".icon").css("transform","rotate(0deg)");
        setWidth("add_delivery","add_delivery_list div");
        $("#add_delivery_list").slideDown(300);
        $(this).find(".icon").css("transform","rotate(180deg)");
        var obj_lis = $("#add_delivery_list ul li");
        for(var i = 0;i < obj_lis.length;i ++){
            obj_lis[i].onclick = function () {
                $("#add_delivery").val(this.innerHTML);
                $("#add_delivery_list").slideUp(300);
                $(this).find(".icon").css("transform","rotate(0deg)");
            }
        }
    }
});
$("#add_is_ephedrine").parent().click(function (e){
    // e.stopPropagation(); //阻止冒泡 
    if($("#add_is_ephedrine_list").is(':visible')){
        $("#add_is_ephedrine_list").slideUp(300);
        $(this).find(".icon").css("transform","rotate(0deg)");
    }
    else{
        $(".search_input_option").slideUp(300);
        $(".icon").css("transform","rotate(0deg)");
        setWidth("add_is_ephedrine","add_is_ephedrine_list div");
        $("#add_is_ephedrine_list").slideDown(300);
        $(this).find(".icon").css("transform","rotate(180deg)");
        var obj_lis = $("#add_is_ephedrine_list ul li");
        for(var i = 0;i < obj_lis.length;i ++){
            obj_lis[i].onclick = function () {
                $("#add_is_ephedrine").val(this.innerHTML);
                $("#add_is_ephedrine_list").slideUp(300);
                $(this).find(".icon").css("transform","rotate(0deg)");
            }
        }
    }
});
$("#add_is_prescription").parent().click(function (e){
    // e.stopPropagation(); //阻止冒泡 
    if($("#add_is_prescription_list").is(':visible')){
        $("#add_is_prescription_list").slideUp(300);
        $(this).find(".icon").css("transform","rotate(0deg)");
    }
    else{
        $(".search_input_option").slideUp(300);
        $(".icon").css("transform","rotate(0deg)");
        setWidth("add_is_prescription","add_is_prescription_list div");
        $("#add_is_prescription_list").slideDown(300);
        $(this).find(".icon").css("transform","rotate(180deg)");
        var obj_lis = $("#add_is_prescription_list ul li");
        for(var i = 0;i < obj_lis.length;i ++){
            obj_lis[i].onclick = function () {
                $("#add_is_prescription").val(this.innerHTML);
                $("#add_is_prescription_list").slideUp(300);
                $(this).find(".icon").css("transform","rotate(0deg)");
            }
        }
    }
});
//新增
$("#add").bind("click", function() {
    $('#add_popup').show();
    $(".shandow").show().css('z-index', '11');
    $('body').css({
        "overflow-x":"hidden",
        "overflow-y":"hidden"
    });
});
// 取消新增
$("#add_popup_top div,#add_popup_bottom button:nth-child(2)").bind("click", function() {
    $("#add_popup").hide();
    $(".shandow").hide().css('z-index','-1');
    $('body').css({
        "overflow-x":"auto",
        "overflow-y":"auto"
    });
});
//确定新增
$("#add_popup_bottom button:nth-child(1)").bind("click", function() {
    var drug_id = $("#add_drug_id").val();
    var drug_name = $("#add_drug_name").val();
    var drug_capacity = $("#add_drug_capacity").val();
    var drug_unit = $("#add_drug_unit").val();
    var drug_price = $("#add_drug_price").val();
    var drug_manufacturer = $("#add_drug_manufacturer").val();
    var drug_approval_number = $("#add_drug_approval_number").val();
    var is_ephedrine = $("#add_is_ephedrine").val();
    var is_prescription = $("#add_is_prescription").val();
    var delivery = $("#add_delivery").val();
    switch (is_ephedrine) {
        case "不含":
            is_ephedrine = 0;
            break;
        case "含":
            is_ephedrine = 1;
            break;
    }
    switch (is_prescription) {
        case "非处方药":
            is_prescription = 0;
            break;
        case "处方药":
            is_prescription = 1;
            break;
    }
    //输入合法判定
    if(drug_id == "" || drug_name == "" || drug_price == "" || drug_approval_number == "" || is_ephedrine == "" || is_prescription == "" || delivery == ""){
        alert("请完整填写带有*的药品信息");
        return false;
    }
    else{
        $.ajax({
            type: "post",
            url: "/ysfwpt/addDrug",
            data: {
                drug_id: drug_id,
                drug_name: drug_name,
                drug_capacity: drug_capacity,
                drug_unit: drug_unit,
                drug_price: drug_price,
                drug_manufacturer: drug_manufacturer,
                drug_approval_number: drug_approval_number,
                hospital: "测试医院",
                is_ephedrine: is_ephedrine,
                is_prescription: is_prescription,
                delivery: delivery
            },
            success: function (data) {
                // console.log("success");
                // console.log(data);
                if(data.result == 0){
                    $("#add_popup").hide();
                    $(".shandow").hide().css('z-index','-1');
                    refresh();
                }
                if(data.result == 1){
                    alert("该药品编码已存在");
                    $("#add_drug_id").val("");
                }
            },
            error: function (data) {
                // console.log("error");
                // console.log(data);
            }
        });
    }
});

var refresh = function () {
    var hospital = $("#hospital").val();
    var delivery = $("#delivery").val();
    //                            用户名            医院           配送方式           查询词            页码          数据数目
    window.location.href = "?" + username + "&" + hospital + "&" + delivery + "&" + searchword + "&" + pageNo + "&" + pageSize;
};